from wordcloud import WordCloud  
# import matplotlib.pyplot as plt  
import jieba  
import sys

import hashlib


def md5_encrypt(input_string):  
    # 创建一个md5对象  
    md5_hash = hashlib.md5()  
      
    # 提供需要加密的字符串，必须是bytes类型  
    # 如果输入的是字符串，需要先编码为bytes  
    if not isinstance(input_string, bytes):  
        input_string = input_string.encode()  
          
    # 使用update方法传入字符串进行加密  
    md5_hash.update(input_string)  
      
    # 使用hexdigest方法获取加密后的16进制字符串  
    hex_dig = md5_hash.hexdigest()  
      
    return hex_dig  

  
# 发送HTTP请求获取网页内容  
output_path = "static/output/image"
text = sys.argv[2]
file_name = md5_encrypt(sys.argv[1])
font_path = '/Users/wangjun/Library/Fonts/AlimamaShuHeiTi-Bold.ttf' 
  
# 中文分词  
words = jieba.cut(text)  
# 创建词云对象  
wordcloud = WordCloud(font_path=font_path, background_color='white').generate(' '.join(words))  
  
# wordcloud.to_image()
wordcloud.to_file(f"{output_path}/{file_name}.png")
# 显示词云图  
# plt.figure(figsize=(10, 10))  # 设置图像大小  
# plt.imshow(wordcloud, interpolation='bilinear')  
# plt.axis("off")  
# plt.show()  
# 保存词云图到本地  
# plt.savefig(f"./output_{file_name}.png", bbox_inches='tight', dpi=500) 
# plt.close()
# plt.savefig(f"./output.png", dpi=500)